package com.wlhse.service.impl;

import com.github.pagehelper.PageHelper;
import com.wlhse.dao.ProblemDescriptionDao;
import com.wlhse.entity.ProblemDescriptionPojo;
import com.wlhse.exception.WLHSException;
import com.wlhse.service.ProblemDescriptionService;
import com.wlhse.util.R;
import com.wlhse.util.SortCodeUtil;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ProblemDescriptionServiceImpl implements ProblemDescriptionService {

    @Resource
    private ProblemDescriptionDao problemDescriptionDao;

    @Resource
    private SortCodeUtil sortCodeUtil;

    @Resource
    private ProblemDescriptionPojo pojo;

    @Override
    @Transactional
    public String addProblemDescription(ProblemDescriptionPojo pojo) {
        String s = problemDescriptionDao.queryLastCode();
        if (StringUtils.isNotBlank(s)) {
            pojo.setProblemDescriptionCode(sortCodeUtil.getMaxCodeString(s));
        } else {
            pojo.setProblemDescriptionCode("0001");
        }
        if (problemDescriptionDao.addProblemDescription(pojo) <= 0)
            throw new WLHSException("新增失败");
        return NR.r();
    }

    @Override
    @Transactional
    public String deleteProblemDescription(int id) {
        if (problemDescriptionDao.deleteProblemDescription(id) <= 0)
            throw new WLHSException("删除失败");
        return NR.r();
    }

    @Override
    @Transactional
    public String updateProblemDescription(ProblemDescriptionPojo pojo) {
        if (problemDescriptionDao.updateProblemDescription(pojo) < 0)
            throw new WLHSException("更新失败");
        return NR.r();
    }

    @Override
    public R queryProblemDescriptionsById(int id) {
        ProblemDescriptionPojo problemDescription = problemDescriptionDao.queryProblemDescriptionsById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("data", problemDescription);
        String[] str = pojo.getScorePenalty(problemDescription.getProblemRank());
        map.put("score", str[0]);
        map.put("penalty", str[1]);
        return R.ok(map);
    }

    // 分页
    @Override
    public String queryProblemDescriptions(ProblemDescriptionPojo pojo) {
        //查询所有
        if (pojo.getType().equals("all"))
            return NR.r(problemDescriptionDao.queryProblemDescriptions(pojo));
        //查询total
        int total = problemDescriptionDao.queryTotal();
        PageHelper.startPage(pojo.getPageIdx(), pojo.getPageSize());
        // 查询数据
        List<ProblemDescriptionPojo> list = problemDescriptionDao.queryProblemDescriptions(pojo);
        // 分页
        return NR.r(list, total, pojo.getPageIdx());
    }

    @Override
    public String queryProblemDescriptionsByDescription(ProblemDescriptionPojo pojo) {
        List<ProblemDescriptionPojo> dataList = problemDescriptionDao.queryProblemDescriptionsByDescription(pojo.getDescription());
        int total = problemDescriptionDao.queryTotal2(pojo.getDescription());
        PageHelper.startPage(pojo.getPageIdx(), pojo.getPageSize());
        return NR.r(dataList, total, pojo.getPageIdx());
    }
}
